package controller;

import java.util.ArrayList;
import java.util.List;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import model.IEBenchmarkProcess;
import model.scaling.IEBenchmarkScaling;
import view.CenterTabbedPane;
import view.CenterTabbedPane.EBenchmarkTab;
import view.EBenchmarkPanel;
import view.IObserver;
import controller.parameterpanels.CtrCompanyPanel;
import controller.parameterpanels.CtrFacilityPanel;
import controller.parameterpanels.CtrFactoryPanel;
import controller.parameterpanels.CtrIndustrialSectorPanel;
import controller.parameterpanels.CtrLocationPanel;
import controller.parameterpanels.CtrProductPanel;

/**
 * The {@code CtrCenterTabbedPane} controls the behavior of the main
 * {@code JTabbedPane} of this application.
 * @author Oliver Eckstein & Sebastian Perlwitz
 *
 */
public class CtrCenterTabbedPane implements ChangeListener {
    
	/**
	 * The controller's view.
	 */
    private CenterTabbedPane tabbedPane;
    
    /**
     * The referenced calculation model.
     */
    private IEBenchmarkProcess eBenchmark;
    
    //#Neu
    /**
     * The referenced calculation model.
     */
    private IEBenchmarkScaling eBenchmarkScaling;
    
    /**
     * The controller of the information panel.
     */
    private CtrInformationPanel infoPanelController;
    
    /**
     * The setup panel where the user can start manipulating the
     * manufacturing methods.
     */
    private IObserver methodsSetupPanel;
    
    /**
     * To create a new instance of a {@code CtrCenterTabbedPane} and connect
     * it to the model and the controller of the information panel.
     * @param eBenchmark the application's model.
     * @param infoPanelController the controller of the information panel.
     */
    public CtrCenterTabbedPane(            
            IEBenchmarkProcess eBenchmark,
            IEBenchmarkScaling eBenchmarkScaling, //#Neu
            CtrInformationPanel infoPanelController) {
        this.eBenchmark = eBenchmark;
        this.eBenchmarkScaling = eBenchmarkScaling; //#Neu
        this.infoPanelController = infoPanelController;
        
        addTabs();
        eBenchmark.registerObserver(tabbedPane);
        eBenchmarkScaling.registerObserver(tabbedPane); //#Neu
    }
    
    /**
     * To get the view of this controller.
     * @return the controller's view.
     */
    public CenterTabbedPane getView() {        
        return tabbedPane;
    }
    
    /**
     * To create the tabbed pane and initially add two tabs where the user can
     * choose the number of manufacturing methods to be calculated and set
     * them up and further tabs for the scaling calculation. Later on more tabs 
     * will be added.
     */
    private void addTabs() {        
        List<EBenchmarkPanel> panels = new ArrayList<EBenchmarkPanel>();
        
        IControllable cm =
                new CtrChooseMethods(
                        eBenchmark,
                        infoPanelController);
        panels.add(cm.getView());        
        
        IControllable sm =
                new CtrSetupMethods(
                        eBenchmark,
                        eBenchmarkScaling, //#Neu 
                        infoPanelController,
                        this); 
        panels.add(sm.getView());
        
        methodsSetupPanel = sm.getView();
        
        //#Neu
        IControllable ctrFacilityPanel =
        		new CtrFacilityPanel(
        				eBenchmarkScaling,
        				infoPanelController);
        panels.add(ctrFacilityPanel.getView());
        
        //#Neu
        IControllable ctrFactoryPanel =
        		new CtrFactoryPanel(
        				eBenchmarkScaling,
        				infoPanelController);
        panels.add(ctrFactoryPanel.getView());
        
        //#Neu
        IControllable ctrLocationPanel =
        		new CtrLocationPanel(
        				eBenchmarkScaling,
        				infoPanelController);
        panels.add(ctrLocationPanel.getView());
        
        //#Neu
        IControllable ctrCompanyPanel =
        		new CtrCompanyPanel(
        				eBenchmarkScaling,
        				infoPanelController);
        panels.add(ctrCompanyPanel.getView());
        
        //#Neu
        IControllable ctrIndustryPanel =
        		new CtrIndustrialSectorPanel(
        				eBenchmarkScaling,
        				infoPanelController);
        panels.add(ctrIndustryPanel.getView());
        
        //#Neu
        IControllable ctrProductPanel =
        		new CtrProductPanel(
        				eBenchmarkScaling,
        				infoPanelController);
        panels.add(ctrProductPanel.getView());
        
        tabbedPane = new CenterTabbedPane(panels, eBenchmark);
        tabbedPane.addChangeListener(this);
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        // Draw the info panel based on the selected tab.
        EBenchmarkTab selectedTab = tabbedPane.getSelectedTab();
        infoPanelController.getView().drawInfoPanel(selectedTab);                  
        
        // To make sure that the methods setup panel only gets updated
        // when it is selected.
        if(selectedTab == EBenchmarkTab.SETUP_METHODS) {            
            eBenchmark.registerObserver(methodsSetupPanel);
            methodsSetupPanel.update();
            
        } else {
            eBenchmark.removeObserver(methodsSetupPanel);
        }
    }
}
